﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TrangTrai.Data.DAO;

namespace Trangtrai.report
{
    public partial class ReportSummary : System.Web.UI.Page
    {
        static SeasonInfo[] arrSeasons = null;
        static AoInfo[] arrAoInfo = null;
        GroupManagementDAO dao = new GroupManagementDAO();
        ReportDAO reportDao = new ReportDAO();
        AoManageDAO aoDao = new AoManageDAO();
        static int aoSeasonId = -1;
        DateTime _toDate;
        static HoachatReport []hcReport = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                arrSeasons = dao.getSeasonList();
                arrAoInfo = dao.getActiveAoList();
                cbAoCode.DataSource = arrAoInfo;
                cbAoCode.DataBind();
                cbSeason.DataSource = arrSeasons;
                cbSeason.DataBind();
                grid.ExpandRow(0);
            }
            else {
                grid.DataSource = hcReport;
                grid.DataBind();
            }
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            int aoId = (int)cbAoCode.Value;
            int seasonId = (int)cbSeason.Value;
            aoSeasonId=aoDao.getAoSeasonByAoIdAndSeasonId(aoId, seasonId);
            if (aoSeasonId == -1) return;
            _toDate = (DateTime)toDate.Value;
            // hoa chat report
             hcReport= reportDao.getHoaChatReport(aoSeasonId,_toDate);
             grid.DataSource = hcReport;
             grid.DataBind();
            // summary report
             SummaryReport [] reportSum = reportDao.getSummaryReports(seasonId, _toDate);
            foreach(SummaryReport item in reportSum){
                 SummaryReport[] temp= reportDao.getStandardDataReport(item.numberOfTom, item.tomAge);
                 if(temp.Length>0){
                     item.tomsizeDKInKg = temp[0].tomsizeDKInKg;
                     item.SLDK = temp[0].SLDK;
                     item.tomsizeDKInGam = temp[0].tomsizeDKInGam;
                     item.hstaTT = item.SLTT / item.numberOfTom;
                     item.hstaDK = item.SLDK / item.numberOfTom;
                     item.foodDK = temp[0].foodDK;
                 }    
            }
            gridSumReport.DataSource = reportSum;
             gridSumReport.DataBind();
        }

        protected void grid_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
        {
            if (e.Column.FieldName == "Total")
            {
                double actualValue = (double)e.GetListSourceFieldValue("totalValue");
                e.Value = actualValue;
            }
        }
    }
}
